﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Data.DAO;
using ServiceStack.Text;
using Framework.Library.Core.Config;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Core.Utils.BBCodeParse;
using Framework.Library.Base.Helper;
using Framework.Library.Core.Provider.Payment;

namespace Framework.Portal.Layouts.Client.Components.Teenio
{
    public partial class Detail : BaseControl
    {
        public string MENUNAME = string.Empty;

        public Detail()
        {
        }

        public Detail(JSection section)
        {
            MENUNAME = section.title;
        }

        protected int articleId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["a"], out articleId);
            hddContent.Value = articleId.ToString();
            if (!IsPostBack)
            {                
                if (articleId > 0)
                {
                    TeenioContent info = ManagerFactory.TeenioContentManager.GetInfo(articleId);
                    if (info != null)
                    {
                        int menuId;
                        int.TryParse(Request.QueryString["m"], out menuId);
                        hddTitle.Value = info.CONTENT_HEADLINE;
                        if (menuId == info.MENUID)                            
                            BindData(info);                            
                        else
                            Response.Redirect(cfg.ArticlePath(info.CONTENT_PORTALID, info.MENUID, info.CONTENT_ID));
                    }
                    else
                        Response.Redirect(Configurations.ROOTDIR);
                }
            }
        }

        private void BindData(TeenioContent info)
        {
            //
            if (!string.IsNullOrEmpty(info.CONTENT_AVATAR))
                imgAvatar.ImageUrl = info.CONTENT_AVATAR;            
            lblTitle.Text = info.CONTENT_HEADLINE;
            lblView.Text = ManagerFactory.DAOEntity.GetList<TeenioContentHistory>(new TeenioContentHistory() { AUDIOID =  info.CONTENT_ID}).Count.ToString();
            lblContent.Text = BBCodeUtils.GetParser(info.CONTENT_BODY, ErrorMode.Strict);
            lblCreateDate.Text = DateTimeHelper.formatDateTime(info.CONTENT_MODIFYDDATE);
            if (info.CONTENT_TYPE != (int)TeenioContentType.Audio)
                pnlCheck.Visible = false;
        }

        protected void btnListen_Click(object sender, EventArgs e)
        {
            VcoinService vcoin = new VcoinService();
            if (Session.IsOnline())
            {     
                //Check xem tài khoản có nghe theo tháng hay đã đăng ký nghe bài này chưa                
                Principal info = Session.GetToken();
                if (info != null)
                {
                    int menuId;
                    int.TryParse(Request.QueryString["m"], out menuId);
                    int checkpayment = ManagerFactory.TeenioPaymentManager.CheckPayment(Session.GetAccountId(), Convert.ToInt32(hddContent.Value));
                    //Nếu đã đăng ký
                    if (checkpayment > 0)
                        Response.Redirect(cfg.ArticlePath(Configurations.PORTALID, menuId, (int)TeenioContentType.Audio, Convert.ToInt32(hddContent.Value)));
                    //Nếu chưa 
                    else
                    {
                        int vcoinbalance = vcoin.BuyItem(info.AccessToken, Convert.ToInt32(cfg.ViewCost.VALUE));
                        if (vcoinbalance >= 0)
                        {
                            TeenioPayment payment = new TeenioPayment();
                            payment.TransCode = DateTime.Now.ToString("yyyyMMddHHmmss");
                            payment.Amount = Convert.ToInt32(cfg.ViewCost.VALUE);
                            payment.Status = 1; // Trạng thái thanh toán thành công
                            payment.Item = Convert.ToInt32(hddContent.Value);
                            payment.ItemType = 1; //Thanh toán 1 bài audio
                            payment.PaygateID = info.AccountId;
                            payment.LastUpdateTime = DateTime.Now;
                            payment.Description = "Nghe audio \"" + hddTitle.Value + "\"";
                            payment.PaygateTransID = Configurations.TransID;
                            ManagerFactory.DAOEntity.Insert(payment);
                            Response.Redirect(cfg.ArticlePath(Configurations.PORTALID, menuId, (int)TeenioContentType.Audio, Convert.ToInt32(hddContent.Value)));
                        }
                    }
                }
            }
            else
            {               
                Session["returnUrl"] = Configurations.ROOTPATH + HttpContext.Current.Request.RawUrl;
                Response.Redirect(vcoin.StringAuthentication(HttpUtility.UrlEncode(Configurations.ROOTPATH + HttpContext.Current.Request.RawUrl)));
            }
        }
    }
}